package com.indusfo.spc.service.impl;

import com.indusfo.spc.mapper.RuleWatercodeMapper;
import com.indusfo.spc.mapper.RuleDetailMapper;
import com.indusfo.spc.pojo.RuleWatercode;
import com.indusfo.spc.pojo.RuleDetail;
import com.indusfo.spc.service.BarCodeRules;
import com.indusfo.spc.service.RuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BarCodeRulesImpl implements BarCodeRules {

    @Resource
    private RuleDetailMapper ruleDetailMapper;

    @Resource
    private RuleWatercodeMapper groupMapper;

    @Resource
    private RuleService ruleService;

    @Override
    public List<String> calculate(Integer ruleId , Integer num) {
       return ruleService.otherRule( ruleId, num);
//        List<String> strings = new ArrayList<>();
//        RuleDetail ruleList = new RuleDetail();
//        ruleList.setRuleId(ruleId);
//        List<RuleDetail> ruleLists = ruleDetailMapper.queryRuleDetail(ruleList);
//        String constant = "";// 定值
//        String variable = "";// 变量
//        String stepize = "";// 步长
//        for (RuleDetail rulelist : ruleLists) {
//            if (rulelist.getConstant() == 1) {
//                constant = constant + rulelist.getInitialValue();
//            } else {
//                if (rulelist.getRuleTypeId() == 2) {
//                    String date = "";
//                    SimpleDateFormat df = null;
//                    if (rulelist.getRuleFormatId() == 1) {
//                        df = new SimpleDateFormat("YYYY");//设置日期格式
//                    }
//                    if (rulelist.getRuleFormatId() == 2) {
//                        df = new SimpleDateFormat("YY");//设置日期格式
//                    }
//                    if (rulelist.getRuleFormatId() == 3) {
//                        df = new SimpleDateFormat("MM");//设置日期格式
//                    }
//                    if (rulelist.getRuleFormatId() == 4) {
//                        df = new SimpleDateFormat("dd");//设置日期格式
//                    }
//                    date = df.format(new Date());
//                    constant = constant + date;
//                }
//                if (rulelist.getRuleTypeId() == 4) {
//                    String first = rulelist.getInitialValue();
//                    constant = constant + first;
//                }
//                if (rulelist.getRuleTypeId() == 1) {
//                    String first = rulelist.getInitialValue();
//                    String len = rulelist.getCharLength();
//                    stepize = rulelist.getStepLength();
//                    variable = variable + String.format("%0" + len + "d", Integer.valueOf(first));
//                }
//            }
//        }
//        RuleWatercode codeGroup = new RuleWatercode();
//        codeGroup.setRuleId(ruleId);
//        codeGroup.setStaticData(constant);
//        RuleWatercode codeGroup1 = groupMapper.queryRuleWatercode(codeGroup);
//        if(codeGroup1 != null){
//            variable = codeGroup1.getLastData();
//            strings=List(num,stepize,variable,ruleLists);
//            codeGroup1.setLastData( String.format("%0" + variable.length() + "d", Integer.valueOf(variable) +num*Integer.valueOf(stepize)));
//            groupMapper.updateRuleWatercode(codeGroup1);
//            return strings;
//        }
//        System.out.println(constant);
//        System.out.println(variable);
//        System.out.println(stepize);
//        strings=List(num,stepize,variable,ruleLists);
//        codeGroup.setLastData( String.format("%0" + variable.length() + "d", Integer.valueOf(variable) +num*Integer.valueOf(stepize)));
//        groupMapper.insertRuleWatercode(codeGroup);
//        return strings;
    }

    public List<String> List(Integer num ,String stepize ,String variable ,List<RuleDetail> ruleLists){
        List<String> stringList = new ArrayList<>();
        String variable1 = "";
        for(int i = 0 ;i < num * Integer.valueOf(stepize); i = i + Integer.valueOf(stepize)){
            String s = "";
            for (RuleDetail rulelist: ruleLists) {
                if(rulelist.getConstant()==1){
                    s = s + rulelist.getInitialValue();
                } else{
                    if(rulelist.getRuleTypeId()==2){
                        String  date = "";
                        SimpleDateFormat df = null;
                        if(rulelist.getRuleFormatId()==1){
                            df = new SimpleDateFormat("YYYY");//设置日期格式
                        }
                        if(rulelist.getRuleFormatId()==2){
                            df = new SimpleDateFormat("YY");//设置日期格式
                        }
                        if(rulelist.getRuleFormatId()==3){
                            df = new SimpleDateFormat("MM");//设置日期格式
                        }
                        if(rulelist.getRuleFormatId()==4){
                            df = new SimpleDateFormat("dd");//设置日期格式
                        }
                        date = df.format(new Date());
                        s = s + date;
                    }
                    if(rulelist.getRuleTypeId()==4){
                        String first = rulelist.getInitialValue();
                        s = s + first;
                    }
                    if(rulelist.getRuleTypeId()==1){
                        String len = rulelist.getCharLength();
                        s = s + String.format("%0"+len+"d", Integer.valueOf(variable) + i);
                        variable1 = String.format("%0"+len+"d", Integer.valueOf(variable) + i);
                    }
                }
            }
            stringList.add(s);
        }
        System.out.println(variable1);
        return stringList;
    }


}
